<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />

        <el-tree :data="data" :props="defaultProps" :default-expand-all="true">
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>

      <add-dialog
        ref="addDept"
        :show-dialog.sync="showDialog"
        :tree-node="treeNode"
        @addDepts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDialog from './components/add-dialog.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'

export default {
  components: {
    TreeTools,
    AddDialog
  },
  data () {
    return {
      company: {},
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      treeNode: {}
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    async getDepartments () {
      const result = await getDepartments()

      this.company = { name: result.companyName, manager: '负责人', id: '' }

      const data = tranListToTreeData(result.depts, '')

      this.data = data
    },
    addDepts (treeNode) {
      this.treeNode = treeNode
      this.showDialog = true
    },
    editDepts (treeNode) {
      this.showDialog = true
      this.treeNode = treeNode

      this.$refs.addDept.getDepartDetail(treeNode.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
